//
//  VideoCoverPicker.m
//  GcwSDKDemo
//
//  Created by 陈超 on 2017/7/18.
//  Copyright © 2017年 陈超. All rights reserved.
//

#import "VideoCoverPicker.h"
#import "AppDelegate.h"
#import <AVFoundation/AVFoundation.h>
@interface VideoCoverPicker ()
@property (weak, nonatomic) IBOutlet UIImageView *coverImage;

@end

@implementation VideoCoverPicker

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIImage *image=[self assetGetThumImage:0.5];
    [self.coverImage setImage:image];
}
- (IBAction)onSliderValueChanged:(UISlider *)sender {
    float value=sender.value;
    UIImage *image=[self assetGetThumImage:value];
    [self.coverImage setImage:image];
    AppDelegate *delegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.tmpVideoFrame=image ;
}
- (UIImage *)assetGetThumImage:(float)timeValue
{
    AVURLAsset *urlSet = [AVURLAsset assetWithURL:self.inputUrl];
   
    CMTime dur=urlSet.duration;
    CMTime atTime=CMTimeMake(dur.value*timeValue, dur.timescale);
    AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:urlSet];
    
    NSError *error = nil;
    
    CMTime actucalTime; //缩略图实际生成的时间
    CGImageRef cgImage = [imageGenerator copyCGImageAtTime:atTime actualTime:&actucalTime error:&error];
    if (error) {
        NSLog(@"截取视频图片失败:%@",error.localizedDescription);
    }
    CMTimeShow(actucalTime);
    UIImage *image = [UIImage imageWithCGImage:cgImage];
    //UIImageWriteToSavedPhotosAlbum(image,nil, nil,nil);
    CGImageRelease(cgImage);
    
    return image;
}
- (IBAction)confirm:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
